package com.yzion.baselib.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.viewbinding.ViewBinding
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.BusUtils
import com.blankj.utilcode.util.ThreadUtils
import com.yzion.baselib.R
import com.yzion.baselib.activity.BaseActivity
import com.yzion.baselib.app.BaseApp
import com.yzion.baselib.dialog.BaseLoadingDialog
import com.yzion.baselib.app.IAppProvider

/**
 * 默认样式的DialogFragment，弹窗
 */
abstract class BaseDialogFragment<VB : ViewBinding> :
    DialogFragment() {

    private var baseLoadingDialog: BaseLoadingDialog? = null

    lateinit var binding: VB

    /**
     * 实例化对应的ViewBinding
     */
    abstract fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): VB

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = getViewBinding(inflater, container, savedInstanceState)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        baseLoadingDialog =
            (requireContext().applicationContext as IAppProvider).createLoadingDialog(requireContext()) as BaseLoadingDialog
        initView()
        if (dialog != null) {
            dialog!!.setCanceledOnTouchOutside(true)
        }
        BusUtils.register(this)
        initData()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        BusUtils.unregister(this)
    }

    protected abstract fun initView()

    abstract fun initData()

    fun showLoading(text: String? = null): BaseLoadingDialog? {
        ThreadUtils.runOnUiThread {
            try {
                baseLoadingDialog?.setLoadingTip(text)?.show()
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
        return baseLoadingDialog
    }

    fun hideLoading() {
        ThreadUtils.runOnUiThread {
            try {
                baseLoadingDialog?.dismiss()
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.CustomDialog)
    }

    fun show() {
        try {
            if (ActivityUtils.getTopActivity() is BaseActivity) {
                val a = ActivityUtils.getTopActivity() as BaseActivity
                show(a.supportFragmentManager, this.hashCode().toString())
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun getBaseActivity(): BaseActivity? {
        try {
            if (activity is BaseActivity) {
                return activity as BaseActivity
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }
}